package institution

import (
	"net/http"

	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/EDDYCJY/go-gin-example/pkg/e"
	"github.com/EDDYCJY/go-gin-example/pkg/util"
	"github.com/EDDYCJY/go-gin-example/service/institution_service"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

type GetGoodsVerify struct {
}

func GetGoods(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form GetGoodsVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := institution_service.GetGoods{
		InstitutionID: InstitutionID,
		PageNum:       util.GetPage(c),
		PageSize:      util.GetLimit(c),
	}
	data, err := Service.GetGoods()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type AddGoodsVerify struct {
	GoodsName    string `alias:"商品名称" form:"goods_name"  json:"goods_name" valid:"Required;"`
	GoodsSpec    string `alias:"商品规格" form:"goods_spec"  json:"goods_spec" valid:"Required;"`
	GoodsNo      string `alias:"商品编码" form:"goods_no"  json:"goods_no" valid:"Required;"`
	SupplyPrice  int    `alias:"供货价" form:"supply_price"  json:"supply_price" `
	SellingPrice int    `alias:"卖货价" form:"selling_price"  json:"selling_price" `
	GoodsImg     string `alias:"商品图片" form:"goods_img"  json:"goods_img" valid:"Required;"`
	GoodsNum     int    `alias:"商品数量" form:"goods_num"  json:"goods_num" valid:"Required;"`
	TagIds       string `alias:"标签" form:"tag_ids"  json:"tag_ids" `
}

func AddGoods(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form AddGoodsVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	UserID := c.MustGet("user_id").(int)

	Service := institution_service.AddGoods{
		InstitutionID: InstitutionID,
		UserID:        UserID,
		GoodsName:     form.GoodsName,
		GoodsSpec:     form.GoodsSpec,
		GoodsNo:       form.GoodsNo,
		SupplyPrice:   form.SupplyPrice,
		SellingPrice:  form.SellingPrice,
		GoodsImg:      form.GoodsImg,
		GoodsNum:      form.GoodsNum,
		TagIds:        form.TagIds,
	}
	data, err := Service.AddGoods()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type EditGoodsVerify struct {
	ID           int    `form:"id" valid:"Required;"`
	GoodsName    string `alias:"商品名称" form:"goods_name"  json:"goods_name" valid:"Required;"`
	GoodsSpec    string `alias:"商品规格" form:"goods_spec"  json:"goods_spec" valid:"Required;"`
	GoodsNo      string `alias:"商品编码" form:"goods_no"  json:"goods_no" valid:"Required;"`
	SupplyPrice  int    `alias:"供货价" form:"supply_price"  json:"supply_price" `
	SellingPrice int    `alias:"卖货价" form:"selling_price"  json:"selling_price" `
	GoodsImg     string `alias:"商品图片" form:"goods_img"  json:"goods_img" valid:"Required;"`
	GoodsNum     int    `alias:"商品数量" form:"goods_num"  json:"goods_num" valid:"Required;"`
	TagIds       string `alias:"标签" form:"tag_ids"  json:"tag_ids" `
}

func EditGoods(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form = EditGoodsVerify{ID: com.StrTo(c.Param("id")).MustInt()}
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := institution_service.EditGoods{
		GoodsID:       form.ID,
		InstitutionID: InstitutionID,
		GoodsName:     form.GoodsName,
		GoodsSpec:     form.GoodsSpec,
		GoodsNo:       form.GoodsNo,
		SupplyPrice:   form.SupplyPrice,
		SellingPrice:  form.SellingPrice,
		GoodsImg:      form.GoodsImg,
		GoodsNum:      form.GoodsNum,
		TagIds:        form.TagIds,
	}
	data, err := Service.EditGoods()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type EditGoodsStreamerVerify struct {
	ID         int `form:"id" valid:"Required;"`
	StreamerID int `alias:"主播ID" form:"streamer_id"  json:"streamer_id" valid:"Required;"`
}

func EditGoodsStreamer(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form = EditGoodsStreamerVerify{ID: com.StrTo(c.Param("id")).MustInt()}
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := institution_service.EditGoodsStreamer{
		GoodsID:       form.ID,
		InstitutionID: InstitutionID,
		StreamerID:    form.StreamerID,
	}
	data, err := Service.EditGoodsStreamer()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type DeleteGoodsVerify struct {
	ID           int    `form:"id" valid:"Required;"`
	GoodsName    string `alias:"商品名称" form:"goods_name"  json:"goods_name" valid:"Required;"`
	GoodsSpec    string `alias:"商品规格" form:"goods_spec"  json:"goods_spec" valid:"Required;"`
	GoodsNo      string `alias:"商品编码" form:"goods_no"  json:"goods_no" valid:"Required;"`
	SupplyPrice  int    `alias:"供货价" form:"supply_price"  json:"supply_price" `
	SellingPrice int    `alias:"卖货价" form:"selling_price"  json:"selling_price" `
	GoodsImg     string `alias:"商品图片" form:"goods_img"  json:"goods_img" valid:"Required;"`
	GoodsNum     int    `alias:"商品数量" form:"goods_num"  json:"goods_num" valid:"Required;"`
}

func DeleteGoods(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form = DeleteGoodsVerify{ID: com.StrTo(c.Param("id")).MustInt()}
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := institution_service.DeleteGoods{
		GoodsID:       form.ID,
		InstitutionID: InstitutionID,
	}
	data, err := Service.DeleteGoods()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}
